package 剑指Offer.medium;

import java.util.Arrays;

public class 表示数值的字符串 {
    class Solution {
        public boolean isNumber(String s) {
            char[] word = s.toCharArray();
//            if (word[0] == 'e') return false;//特例判断，开头为e不为数字
            return charge(word);
        }

        private boolean charge(char[] word) {
            //如果是符号位，后面应是数字
            if (word[0] == '+' || word[0] == '-') {
                return charge(Arrays.copyOfRange(word, 1, word.length));
            }

            int count = 0;
            for (int i = 0; i < word.length; i++) {
                if (word[i] == 'e' || word[i] == '.') {
                    char[] left = Arrays.copyOfRange(word, 0, i);
                    char[] right = Arrays.copyOfRange(word, i + 1, word.length);
                    if (left == null || right == null || right[0] == '+') return false;
                    //如果某一位为e或小数点，左右都需是数字
                    return charge(left) && charge(right);
                }
                if (word[i] >= '0' && word[i] <= '9') {
                    count++;
                }
            }
            //如果全是数字位满足条件
            return count == word.length ? true : false;
        }
    }
}
